package com.hx.er.common.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.bean.base.LanguageBean;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.common.ILanguageService;
import com.opensymphony.xwork2.ActionContext;

public class ErLanguages {
	private static Map<Long, LanguageBean> langIdBeanMap;
	private static Map<String, LanguageBean> langCodeBeanMap;
	private static List<LanguageBean> langList;
	private static List<Long> langIdList;

	@Autowired
	private ILanguageService languageService;

	public void load() {
		langIdBeanMap = new HashMap<Long, LanguageBean>();
		langCodeBeanMap = new HashMap<String, LanguageBean>();
		langList = languageService.getLangList();
		langIdList = new ArrayList<>();
		
		for (LanguageBean lang : langList) {
			langIdBeanMap.put(lang.getLangId(), lang);
			langCodeBeanMap.put(lang.getLangCode(), lang);
			langIdList.add(lang.getLangId());
			System.out.println(lang.getLangCode());
		}
	}

	public void reload() {
		this.load();
	}

	public static Locale getLocale(long langId) {
		LanguageBean langBean = langIdBeanMap.get(langId);
		if (StringUtil.isEqual(langBean.getLangCode(), "ja_JP")) {
			return Locale.JAPANESE;
		} else if (StringUtil.isEqual(langBean.getLangCode(), "zh_CN")) {
			return Locale.SIMPLIFIED_CHINESE;
		} else {
			return Locale.ENGLISH;
		}
	}

	public static Long getCurrentLangId() {
		// 获取当前语言编号
		Locale locale = ActionContext.getContext().getLocale();
		LanguageBean langBean = langCodeBeanMap.get(locale.toString());

		if (langBean == null) {
			return null;
		} else {
			return langBean.getLangId();
		}
	}

	public static List<LanguageBean> getLangList() {
		return langList;
	}

	public static List<Long> getLangIdList() {
		return langIdList;
	}

}
